home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2004 April / Gamestar_61_2004-04_dvdb.iso / DVDStar / Editace / hltp.exe / {app} / Applications / QuArK / plugins / mapgridscale.py < prev    next >
Text File  |  2004-01-05  |  17KB  |  469 lines

  1. """   QuArK  -  Quake Army Knife
  2.  
  3. Map editor views, grid scale numbering feature.
  4. """
  5. #
  6. # Copyright (C) 1996-99 Armin Rigo
  7. # THIS FILE IS PROTECTED BY THE GNU GENERAL PUBLIC LICENCE
  8. # FOUND IN FILE "COPYING.TXT"
  9. #
  10.  
  11. #$Header: /cvsroot/quark/runtime/plugins/mapgridscale.py,v 1.4 2003/12/18 21:51:46 peter-b Exp $
  12.  
  13. Info = {
  14.    "plug-in":       "Display grid scale",
  15.    "desc":          "Displays the grid scale in the 2D viewing windows. Activate from the 'Options' menu",
  16.    "date":          "Dec. 13 2003",
  17.    "author":        "cdunde",
  18.    "author e-mail": "cdunde1@comcast.net",
  19.    "quark":         "Version 6" }
  20.  
  21.  
  22. import quarkpy.mapoptions
  23. from quarkpy.maputils import *
  24.  
  25.  
  26. #
  27. # -------- grid numbering routines
  28. #
  29.  
  30. def NumberGrid(cv, view, text):
  31.     "function to place numbers on grid"
  32.     editor = mapeditor()
  33.     cv.textout(view.y, view.z, text)
  34.  
  35. #
  36. # This part is a magical incantation.
  37. # First the normal arguments for
  38. #  finishdrawing, then the oldfinish=... stuff,
  39. #  which has the effect of storing the current
  40. #  finishdrawing method inside this function as
  41. #  the value of the oldfinish variable.
  42. # These def statements are executed as the plugins are being
  43. #  loaded, but not reexecuted in later operations
  44. #  of the map editor, only the functions they define are.
  45. #
  46.  
  47. def gridfinishdrawing(editor, view, gridoldfinish=quarkpy.mapeditor.MapEditor.finishdrawing):
  48.  
  49.     #
  50.     # execute the old method
  51.     #
  52.  
  53.     gridoldfinish(editor, view)
  54.  
  55.     #
  56.     # Below test if the grid is even on
  57.     #
  58.  
  59.     if editor.grid == 0:return
  60.  
  61.     def MyMakeScroller(layout, view):
  62.         sbviews = [None, None]
  63.         for ifrom, linkfrom, ito, linkto in layout.sblinks:
  64.             if linkto is view:
  65.                 sbviews[ito] = (ifrom, linkfrom)
  66.         def scroller(x, y, view=view, hlink=sbviews[0], vlink=sbviews[1]):
  67.             view.scrollto(x, y)
  68.             if hlink is not None:
  69.                 if hlink[0]:
  70.                     hlink[1].scrollto(None, x)
  71.                 else:
  72.                     hlink[1].scrollto(x, None)
  73.             if vlink is not None:
  74.                 if vlink[0]:
  75.                     vlink[1].scrollto(None, y)
  76.                 else:
  77.                     vlink[1].scrollto(y, None)
  78.             if not MapOption("AxisXYZ") and not MapOption("All2DviewsScale") and not MapOption("XviewScale") and not MapOption("YviewScale") and not MapOption("ZviewScale"):
  79.                 view.update()
  80.             else:
  81.                 view.repaint()
  82.         return scroller
  83.     quarkpy.qhandles.MakeScroller = MyMakeScroller
  84.  
  85.  
  86. # The following sets the canvas function to draw the images.
  87.  
  88.     cv = view.canvas()
  89.  
  90.     grid = editor.gridstep    # Gives grid setting
  91.     gridunits = quarkx.ftos(grid) # Converts float nbr to string
  92.     type = view.info["type"]  # These type values are set
  93.                               #  in the layout-defining plugins.
  94.  
  95.  
  96.     if type == "YZ":
  97.  
  98.        if not MapOption("XviewScale") and not MapOption("All2DviewsScale"):
  99.            return
  100.  
  101.        YZarea = `view.clientarea`      # Gets the view area as a string
  102.        pixels = YZarea.replace("(","")   # trims ( from YZarea
  103.        pixels = pixels.replace(")","")  # trims ) from YZarea
  104.        pixels = pixels.split(",")       # trims , from YZarea
  105.        Ystring = pixels[0]                 # pulls out y factor string
  106.        Zstring = pixels[1].strip()   # pulls out z factor string
  107.        Ypixels = int(Ystring)     # converts y string to intiger nbr
  108.        Zpixels = int(Zstring)     # converts z string to intiger nbr
  109.        highlight = int(quarkx.setupsubset(SS_MAP, "Display")["GridHighlight"])
  110.        Ygroups = ((Ypixels/(grid * 1.0)) / view.scale()) / highlight
  111.        Zgroups = ((Zpixels/(grid * 1.0)) / view.scale()) / highlight
  112.        pixspergroup = Zpixels / Zgroups
  113.        Ycounter = 1
  114.        Zcounter = 1
  115.        Ygroup = (Ypixels / Ygroups)
  116.        Zgroup = (Zpixels / Zgroups)
  117.        if Ygroup < 20:return
  118.        units = (grid * highlight)
  119.        Ystring = quarkx.ftos(0)
  120.        Zstring = quarkx.ftos(0)
  121.        Yviewcenter = (Ypixels/2)+4
  122.        Zviewcenter = (Zpixels/2)-4
  123.        Ygroup1 = Yviewcenter+2
  124.        Zgroup1 = Zviewcenter+2
  125.        cv.brushstyle = BS_CLEAR
  126.        cv.fontname = "Terminal"
  127.        cv.textout(Yviewcenter, 2, "Y " + Ystring)
  128.        cv.textout(0, Zviewcenter, "Z " + Zstring)
  129.        Ytotal =  (units * 2)
  130.        Ztotal =  units
  131.        if pixspergroup > 40:
  132.            Zgroup = Zgroup/2
  133.            Ztotal = Ztotal/2
  134.            units = units/2
  135.        if pixspergroup > 80:
  136.            Zgroup = Zgroup/2
  137.            Ztotal = Ztotal/2
  138.            units = units/2
  139.        if pixspergroup > 160:
  140.            Zgroup = Zgroup/2
  141.            Ztotal = Ztotal/2
  142.            units = units/2
  143.        while 1:
  144.            if Zcounter > 11:
  145.               break
  146.            else:
  147.                Zstring =  quarkx.ftos(Ztotal)
  148.                Znextgroupup = Zgroup1 - (Zgroup * Zcounter)
  149.                if Znextgroupup > 19:
  150.                    cv.textout(0, Znextgroupup, " " + Zstring)
  151.                Znextgroupdown = Zgroup1 + (Zgroup * Zcounter)
  152.                cv.textout(0, Znextgroupdown, "-" + Zstring)
  153.                Zcounter = Zcounter + 1
  154.                Ztotal = Ztotal + units
  155.  
  156.        if pixspergroup > 40:
  157.            Ygroup = Ygroup/2
  158.            Ytotal = Ytotal/2
  159.        if pixspergroup > 80:
  160.            Ygroup = Ygroup/2
  161.            Ytotal = Ytotal/2
  162.        if pixspergroup > 160:
  163.            Ygroup = Ygroup/2
  164.            Ytotal = Ytotal/2
  165.        if pixspergroup > 320:
  166.            Ygroup = Ygroup/2
  167.            Ytotal = Ytotal/2
  168.            units = units*.5
  169.        while 1:
  170.            if Ycounter > 7:
  171.                break
  172.            else:
  173.                Ystring =  quarkx.ftos(Ytotal)
  174.                Ynextgroupleft = Ygroup1 - ((Ygroup*2) * Ycounter)
  175.                if not MapOption("AxisXYZ"):
  176.                    cv.textout(Ynextgroupleft-2, 2, "-" + Ystring)
  177.                else:
  178.                    if Ynextgroupleft > 40:
  179.                        cv.textout(Ynextgroupleft-2, 2, "-" + Ystring)
  180.                Ynextgroupright = Ygroup1 + ((Ygroup*2) * Ycounter)
  181.                cv.textout(Ynextgroupright-2, 2, Ystring)
  182.                Ycounter = Ycounter + 1
  183.                Ytotal = Ytotal + (units*2)
  184.  
  185.  
  186.     elif type == "XZ":
  187.  
  188.        if not MapOption("All2DviewsScale") and not MapOption("YviewScale"):
  189.            return
  190.  
  191.        XZarea = `view.clientarea`
  192.        pixels = XZarea.replace("(","")
  193.        pixels = pixels.replace(")","")
  194.        pixels = pixels.split(",")
  195.        Xstring = pixels[0]
  196.        Zstring = pixels[1].strip()
  197.        Xpixels = int(Xstring)
  198.        Zpixels = int(Zstring)
  199.        highlight = int(quarkx.setupsubset(SS_MAP, "Display")["GridHighlight"])
  200.        Xgroups = ((Xpixels/(grid * 1.0)) / view.scale()) / highlight
  201.        Zgroups = ((Zpixels/(grid * 1.0)) / view.scale()) / highlight
  202.        pixspergroup = Zpixels / Zgroups
  203.        Xcounter = 1
  204.        Zcounter = 1
  205.        Xgroup = (Xpixels / Xgroups)
  206.        Zgroup = (Zpixels / Zgroups)
  207.        if Xgroup < 20:return
  208.        units = (grid * highlight)
  209.        Xstring = quarkx.ftos(0)
  210.        Zstring = quarkx.ftos(0)
  211.        Xviewcenter = (Xpixels/2)+4
  212.        Zviewcenter = (Zpixels/2)-4
  213.        Xgroup1 = Xviewcenter+2
  214.        Zgroup1 = Zviewcenter+2
  215.        cv.brushstyle = BS_CLEAR
  216.        cv.fontname = "Terminal"
  217.        cv.textout(Xviewcenter, 2, "X " + Xstring)
  218.        cv.textout(0, Zviewcenter, "Z " + Zstring)
  219.        Xtotal =  (units * 2)
  220.        Ztotal =  units
  221.        if pixspergroup > 40:
  222.            Zgroup = Zgroup/2
  223.            Ztotal = Ztotal/2
  224.            units = units/2
  225.        if pixspergroup > 80:
  226.            Zgroup = Zgroup/2
  227.            Ztotal = Ztotal/2
  228.            units = units/2
  229.        if pixspergroup > 160:
  230.            Zgroup = Zgroup/2
  231.            Ztotal = Ztotal/2
  232.            units = units/2
  233.        while 1:
  234.            if Zcounter > 11:
  235.               break
  236.            else:
  237.                Zstring =  quarkx.ftos(Ztotal)
  238.                Znextgroupup = Zgroup1 - (Zgroup * Zcounter)
  239.                if Znextgroupup > 19:
  240.                    cv.textout(0, Znextgroupup, " " + Zstring)
  241.                Znextgroupdown = Zgroup1 + (Zgroup * Zcounter)
  242.                cv.textout(0, Znextgroupdown, "-" + Zstring)
  243.                Zcounter = Zcounter + 1
  244.                Ztotal = Ztotal + units
  245.  
  246.        if pixspergroup > 40:
  247.            Xgroup = Xgroup/2
  248.            Xtotal = Xtotal/2
  249.        if pixspergroup > 80:
  250.            Xgroup = Xgroup/2
  251.            Xtotal = Xtotal/2
  252.        if pixspergroup > 160:
  253.            Xgroup = Xgroup/2
  254.            Xtotal = Xtotal/2
  255.        if pixspergroup > 320:
  256.            Xgroup = Xgroup/2
  257.            Xtotal = Xtotal/2
  258.            units = units*.5
  259.        while 1:
  260.            if Xcounter > 7:
  261.                break
  262.            else:
  263.                Xstring =  quarkx.ftos(Xtotal)
  264.                Xnextgroupleft = Xgroup1 - ((Xgroup*2) * Xcounter)
  265.                if not MapOption("AxisXYZ"):
  266.                    cv.textout(Xnextgroupleft-2, 2, "-" + Xstring)
  267.                else:
  268.                    if Xnextgroupleft > 40:
  269.                        cv.textout(Xnextgroupleft-2, 2, "-" + Xstring)
  270.                Xnextgroupright = Xgroup1 + ((Xgroup*2) * Xcounter)
  271.                cv.textout(Xnextgroupright-2, 2, Xstring)
  272.                Xcounter = Xcounter + 1
  273.                Xtotal = Xtotal + (units*2)
  274.  
  275.  
  276.     elif type == "XY":
  277.  
  278.        if not MapOption("All2DviewsScale") and not MapOption("ZviewScale"):
  279.            return
  280.  
  281.        XZarea = `view.clientarea`
  282.        pixels = XZarea.replace("(","")
  283.        pixels = pixels.replace(")","")
  284.        pixels = pixels.split(",")
  285.        Xstring = pixels[0]
  286.        Ystring = pixels[1].strip()
  287.        Xpixels = int(Xstring)
  288.        Ypixels = int(Ystring)
  289.        highlight = int(quarkx.setupsubset(SS_MAP, "Display")["GridHighlight"])
  290.        Xgroups = ((Xpixels/(grid * 1.0)) / view.scale()) / highlight
  291.        Ygroups = ((Ypixels/(grid * 1.0)) / view.scale()) / highlight
  292.        pixspergroup = Ypixels / Ygroups
  293.        Xcounter = 1
  294.        Ycounter = 1
  295.        Xgroup = (Xpixels / Xgroups)
  296.        Ygroup = (Ypixels / Ygroups)
  297.        if Xgroup < 20:return
  298.        units = (grid * highlight)
  299.        Xstring = quarkx.ftos(0)
  300.        Ystring = quarkx.ftos(0)
  301.        Xviewcenter = (Xpixels/2)+4
  302.        Yviewcenter = (Ypixels/2)-4
  303.        Xgroup1 = Xviewcenter+2
  304.        Ygroup1 = Yviewcenter+2
  305.        cv.brushstyle = BS_CLEAR
  306.        cv.fontname = "Terminal"
  307.        cv.textout(Xviewcenter, 2, "X " + Xstring)
  308.        cv.textout(0, Yviewcenter, "Y " + Ystring)
  309.        Xtotal =  (units * 2)
  310.        Ytotal =  units
  311.        if pixspergroup > 40:
  312.            Ygroup = Ygroup/2
  313.            Ytotal = Ytotal/2
  314.            units = units/2
  315.        if pixspergroup > 80:
  316.            Ygroup = Ygroup/2
  317.            Ytotal = Ytotal/2
  318.            units = units/2
  319.        if pixspergroup > 160:
  320.            Ygroup = Ygroup/2
  321.            Ytotal = Ytotal/2
  322.            units = units/2
  323.        while 1:
  324.            if Ycounter > 11:
  325.               break
  326.            else:
  327.                Ystring =  quarkx.ftos(Ytotal)
  328.                Ynextgroupup = Ygroup1 - (Ygroup * Ycounter)
  329.                if Ynextgroupup > 19:
  330.                    cv.textout(0, Ynextgroupup, " " + Ystring)
  331.                Ynextgroupdown = Ygroup1 + (Ygroup * Ycounter)
  332.                cv.textout(0, Ynextgroupdown, "-" + Ystring)
  333.                Ycounter = Ycounter + 1
  334.                Ytotal = Ytotal + units
  335.  
  336.        if pixspergroup > 40:
  337.            Xgroup = Xgroup/2
  338.            Xtotal = Xtotal/2
  339.        if pixspergroup > 80:
  340.            Xgroup = Xgroup/2
  341.            Xtotal = Xtotal/2
  342.        if pixspergroup > 160:
  343.            Xgroup = Xgroup/2
  344.            Xtotal = Xtotal/2
  345.        if pixspergroup > 320:
  346.            Xgroup = Xgroup/2
  347.            Xtotal = Xtotal/2
  348.            units = units*.5
  349.        while 1:
  350.            if Xcounter > 7:
  351.                break
  352.            else:
  353.                Xstring =  quarkx.ftos(Xtotal)
  354.                Xnextgroupleft = Xgroup1 - ((Xgroup*2) * Xcounter)
  355.                if not MapOption("AxisXYZ"):
  356.                    cv.textout(Xnextgroupleft-2, 2, "-" + Xstring)
  357.                else:
  358.                    if Xnextgroupleft > 40:
  359.                        cv.textout(Xnextgroupleft-2, 2, "-" + Xstring)
  360.                Xnextgroupright = Xgroup1 + ((Xgroup*2) * Xcounter)
  361.                cv.textout(Xnextgroupright-2, 2, Xstring)
  362.                Xcounter = Xcounter + 1
  363.                Xtotal = Xtotal + (units*2)
  364.  
  365.     else:
  366.        return
  367.  
  368. #
  369. # Now set our new function as the finishdrawing method.
  370. #
  371.  
  372. quarkpy.mapeditor.MapEditor.finishdrawing = gridfinishdrawing
  373.  
  374.  
  375. # ********* This creates the Options menu 2D grid items ***************
  376.  
  377.  
  378. def All2DviewsClick(m):
  379.     editor = mapeditor()
  380.     if not MapOption("All2DviewsScale"):
  381.         quarkx.setupsubset(SS_MAP, "Options")['All2DviewsScale'] = "1"
  382.         quarkx.setupsubset(SS_MAP, "Options")['XviewScale'] = None
  383.         quarkx.setupsubset(SS_MAP, "Options")['YviewScale'] = None
  384.         quarkx.setupsubset(SS_MAP, "Options")['ZviewScale'] = None
  385.     else:
  386.         quarkx.setupsubset(SS_MAP, "Options")['All2DviewsScale'] = None
  387.     editor.invalidateviews()
  388.  
  389. def XviewScaleClick(m):
  390.     editor = mapeditor()
  391.     if not MapOption("XviewScale"):
  392.         quarkx.setupsubset(SS_MAP, "Options")['XviewScale'] = "1"
  393.         quarkx.setupsubset(SS_MAP, "Options")['All2DviewsScale'] = None
  394.     else:
  395.         quarkx.setupsubset(SS_MAP, "Options")['XviewScale'] = None
  396.     editor.invalidateviews()
  397.  
  398. def YviewScaleClick(m):
  399.     editor = mapeditor()
  400.     if not MapOption("YviewScale"):
  401.         quarkx.setupsubset(SS_MAP, "Options")['YviewScale'] = "1"
  402.         quarkx.setupsubset(SS_MAP, "Options")['All2DviewsScale'] = None
  403.     else:
  404.         quarkx.setupsubset(SS_MAP, "Options")['YviewScale'] = None
  405.     editor.invalidateviews()
  406.  
  407. def ZviewScaleClick(m):
  408.     editor = mapeditor()
  409.     if not MapOption("ZviewScale"):
  410.         quarkx.setupsubset(SS_MAP, "Options")['ZviewScale'] = "1"
  411.         quarkx.setupsubset(SS_MAP, "Options")['All2DviewsScale'] = None
  412.     else:
  413.         quarkx.setupsubset(SS_MAP, "Options")['ZviewScale'] = None
  414.     editor.invalidateviews()
  415.  
  416.  
  417.  
  418. def View2DgridMenu(editor):
  419.  
  420.     X0 = quarkpy.qmenu.item("All 2D views", All2DviewsClick, "|All 2D views:\n\nIf this menu item is checked, it will display a scale of the current grid setting in all 2D views and deactivate this menu's individual items.|intro.mapeditor.menu.html#optionsmenu")
  421.  
  422.     X1 = quarkpy.qmenu.item("X-Face 2D view", XviewScaleClick, "|X-Face 2D view:\n\nIf this menu item is checked, it will display a scale of the current grid setting in the ' X - Face ' 2D view and deactivate this menu's  'All 2D views'  item if it is currently checked.|intro.mapeditor.menu.html#optionsmenu")
  423.  
  424.     X2 = quarkpy.qmenu.item("Y-Side 2D view", YviewScaleClick, "|Y-Side 2D view:\n\nIf this menu item is checked, it will display a scale of the current grid setting in the ' Y-Side ' 2D view and deactivate this menu's  'All 2D views'  item if it is currently checked.|intro.mapeditor.menu.html#optionsmenu")
  425.  
  426.     X3 = quarkpy.qmenu.item("Z-Top 2D view", ZviewScaleClick, "|Z-Top 2D view:\n\nIf this menu item is checked, it will display a scale of the current grid setting in the ' Z-Top ' 2D view and deactivate this menu's  'All 2D views'  item if it is currently checked.|intro.mapeditor.menu.html#optionsmenu")
  427.  
  428.     menulist = [X0, X1, X2, X3]
  429.  
  430.     items = menulist
  431.     X0.state = quarkx.setupsubset(SS_MAP,"Options").getint("All2DviewsScale")
  432.     X1.state = quarkx.setupsubset(SS_MAP,"Options").getint("XviewScale")
  433.     X2.state = quarkx.setupsubset(SS_MAP,"Options").getint("YviewScale")
  434.     X3.state = quarkx.setupsubset(SS_MAP,"Options").getint("ZviewScale")
  435.  
  436.     return menulist
  437.  
  438. shortcuts = {}
  439.  
  440.  
  441. # ************************************************************
  442. # ******************Creates the Popup menu********************
  443.  
  444. def ViewAmendMenu1click(m):
  445.     editor = mapeditor(SS_MAP)
  446.     if editor is None: return
  447.     m.items = View2DgridMenu(editor)
  448.  
  449.  
  450. GridMenuCmds = [quarkpy.qmenu.popup("Grid scale in 2D views", [], ViewAmendMenu1click, "|Grid scale in 2D views:\n\nThese functions allow you to display a scale of the current grid setting in any one, combination, or all of the 2D views of the Editor.", "intro.mapeditor.menu.html#optionsmenu")]
  451.     
  452.  
  453. # ----------- REVISION HISTORY ------------
  454. #
  455. #
  456. #$Log: mapgridscale.py,v $
  457. #Revision 1.4  2003/12/18 21:51:46  peter-b
  458. #Removed reliance on external string library from Python scripts (second try ;-)
  459. #
  460. #Revision 1.3  2003/12/18 12:19:42  cdunde
  461. #To add All 2d views feature and auto trun offs
  462. #
  463. #Revision 1.2  2003/12/15 12:47:37  cdunde
  464. #To add menu check marks and zoom feature
  465. #
  466. #Revision 1.1  2003/12/13 22:12:42  cdunde
  467. #To add new Grid in 2D views feature
  468. #
  469. #